﻿using System.Windows;

namespace GenericQuadTree
{
    public class QuadTree<T> where T : TreeObject
    {
        public double TreeWidth { get; private set; }
        public double TreeHeight { get; private set; }
        public double MinNodeWidth { get; private set; }
        public double MinNodeHeight { get; private set; }
        public Point Location { get; private set; }

        private readonly QuadNode<T> _rootNode;

        /// <summary>
        ///   Base quad tree
        /// </summary>
        /// <param name = "treeWidth">Total width of tree</param>
        /// <param name = "treeHeight">Total height of tree</param>
        /// <param name = "minNodeWidth">Minimum node width</param>
        /// <param name = "minNodeHeight">Minimum node height</param>
        /// <param name = "location">Starting location</param>
        public QuadTree(double treeWidth, double treeHeight, double minNodeWidth, double minNodeHeight, Point location)
        {
            TreeWidth = treeWidth;
            TreeHeight = treeHeight;
            MinNodeWidth = minNodeWidth;
            MinNodeHeight = minNodeHeight;
            Location = location;

            //Create the root node for the quad tree.
            _rootNode = new QuadNode<T>(0, location, NodeType.Full, this, null);
        }

        public QuadNode<T> GetNodeAtLocation(Point location)
        {
            return !_rootNode.ContainsPoint(location) ? null : _rootNode.GetNodeAtLocation(location);
        }

        public void AddItem(T item)
        {
            item.UpdateTreeReference(this);
        }
    }
}